package com.jhws.sys.role.service;

import java.util.List;
import com.jhws.core.db.BaseDao;
import com.jhws.sys.role.bo.TblRoleDimBo;
import com.jhws.util.StringUtils;

import javax.annotation.Resource;

public class TblRoleDimServiceSpringImpl implements ITblRoleDimService {
	@Resource
	private BaseDao BaseDao;

	public void save(TblRoleDimBo bo) throws Exception {
		BaseDao.saveOrUpdate(bo);
	}

	public List<TblRoleDimBo> findTblRoleDimByWhere(String whereClause) throws Exception {
		return BaseDao.findList("from TblRoleDimBo "+whereClause);
	}

	public TblRoleDimBo findTbleRoleByRoleName(String roleName) throws Exception{
	    List<TblRoleDimBo> list = BaseDao.findList("from TblRoleDimBo where roleName = '"+roleName+"'");
	    if(list != null && list.size() != 0)
	        return list.get(0);
	    else
	        return null;
    }
	public TblRoleDimBo findTblRoleDimById(String TblRoleId) {
		return BaseDao.get(TblRoleDimBo.class,TblRoleId);
	}

	public void removeRoleDim(String TblRoleId) {
		BaseDao.delete(TblRoleDimBo.class,TblRoleId);
	}

	/* 判断某个角色的祖先中是否包含isRoleName这个角色 */
	public Boolean isRole(String roleName,String isRoleName) throws Exception{
	    if(isRoleName.equals(roleName))
	        return true;
	    TblRoleDimBo role = findTbleRoleByRoleName(roleName);
	    String roleParents = role.getRoleParents();
	    if(StringUtils.isEmpty(roleParents))
	        return false;

	    String [] roles = roleParents.split(",");
        for(String r : roles){
            if(r.equals(isRoleName))
                return true;
        }
        return false;
    }

}
